package  main.java.leetcode.editor.cn;
//2022-05-27 21:24:24
//有个内含单词的超大文本文件，给定任意两个不同的单词，找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次，而每次寻找的单词
//不同，你能对此优化吗? 
//
// 示例： 
//
// 
//输入：words = ["I","am","a","student","from","a","university","in","a","city"], 
//word1 = "a", word2 = "student"
//输出：1 
//
// 提示： 
//
// 
// words.length <= 100000 
// 
// Related Topics 数组 字符串 👍 78 👎 0

class FindClosestLcci {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new FindClosestLcci().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int findClosest(String[] words, String word1, String word2) {

        int n = words.length;
        int start = 0;
        int end = 0;
        int size = Integer.MAX_VALUE;
        for(int i = 0;i<n;i++){
            if(word1.equals(words[i])){
                start = i;
            }
            if(word2.equals(words[i])){
                end = i;
            }
            if(start>0&&end>0){
                size = Math.min(size,Math.abs(end-start));
            }
        }
        return size;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
